<?php

/**
 * This is the model class for table "usuarios".
 *
 * The followings are the available columns in table 'usuarios':
 * @property integer $idUsuario
 * @property string $nombre
 * @property string $password
 * @property integer $rol
 */
class Usuarios extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Usuarios the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuarios';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombre, password, rol', 'required'),
			array('rol', 'numerical', 'integerOnly'=>true),
			array('nombre, password', 'length', 'max'=>45),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idUsuario, nombre, rol', 'safe', 'on'=>'getGridData'),                        
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idUsuario' => 'Id Usuario',
			'nombre' => 'Nombre',
			'password' => 'Password',
			'rol' => 'Rol',
		);
	}        
        public function getGridData($filtersGrid)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.                
                $rawData = Yii::app()->db->createCommand()
                    ->select('idUsuario, nombre, rol')
                    ->from('usuarios')
                    ->queryAll(); 
                
                $filteredData=$filtersGrid->filter($rawData);                
                
                $dataProvider=new CArrayDataProvider($filteredData, array(
                    'keyField'=>'idUsuario',
                    'sort'=>array(
                        'attributes'=>array(
                             'idUsuario', 'nombre', 'rol',
                        ),
                    ),
                    'pagination'=>array(
                        'pageSize'=>5,
                    ),
                ));
                return $dataProvider;
	}
        
        public function getUsuario($nombre)
	{
                $criteria=new CDbCriteria;
                $criteria->select=('nombre, password');
                $criteria->condition=('nombre=\''.$nombre.'\'');
                return Usuarios::model()->findAll($criteria);
	}
}